<?php
/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 * 
 * =================================================================
 *                 MAGENTO EDITION USAGE NOTICE
 * =================================================================
 * This package designed for Magento COMMUNITY edition
 * aheadWorks does not guarantee correct work of this extension
 * on any other Magento edition except Magento COMMUNITY edition.
 * aheadWorks does not provide extension support in case of
 * incorrect edition usage.
 * =================================================================
 *
 * @category   AW
 * @package    AW_Imagetest
 * @copyright  Copyright (c) 2010-2011 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 */
/**
 * Image Testimonial Edit Form Container
 */
class AW_Imagetest_Block_Adminhtml_Image_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{   
    /**
     * Class constructor/
     */
    public function __construct()
    {
        parent::__construct();

        $id = $this->getRequest()->getParam('id');
        $this->_objectId = 'id';
        $this->_blockGroup = 'imagetest';
        $this->_controller = 'adminhtml_image';

        $this->_updateButton('save', 'label', Mage::helper('imagetest')->__('Save Image'));
        $this->_updateButton('delete', 'label', Mage::helper('imagetest')->__('Delete Testimonial'));

        if ($id){
            $this->_addButton('fulldelete', array(
                'label'     => Mage::helper('imagetest')->__('Delete Testimonial and Source'),
                'onclick'   => 'fulldelete()',
                'class'     => 'delete',
            ), 0);
        } else {
            $this->_removeButton('delete');
        }

        $this->_addButton('saveandcontinue', array(
            'label'     => Mage::helper('adminhtml')->__('Save And Continue Edit'),
            'onclick'   => 'saveAndContinueEdit()',
            'class'     => 'save',
        ), -100);

        $params = array('id' => $id);
        # Say about responser
        $params[AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_KEY] = AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_EDIT;
        # Set back route if need it
        if ($this->_isPending()){
            $params[AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_PENDING_KEY] = AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_PENDING_ROUTE;
        }
        if ($this->getRequest()->getParam(AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_KEY) == AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_PRODUCT){
            $params[AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_KEY] = AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_PRODUCT;
            $params['product_id'] = $this->getRequest()->getParam('product_id');
            $params['store'] = $this->getRequest()->getParam('store');
        }
        $fulldeleteUrl = $this->getUrl('*/*/fulldelete', $params);
        $isFile = AW_Imagetest_Model_System_Config_Source_Image_Sourcetype::SOURCE_FILE;
        $isLink = AW_Imagetest_Model_System_Config_Source_Image_Sourcetype::SOURCE_LINK;
        $this->_formScripts[] = "
            function saveAndContinueEdit(){
                editForm.submit($('edit_form').action+'back/edit/');
            }
            function fulldelete(){
                deleteConfirm('Are you sure you want to do this?', '{$fulldeleteUrl}');
            }
            function selectSourceType(){
                var is_file = '{$isFile}';
                var is_link = '{$isLink}';
                var req_class = 'required-entry';
                var val_failed = 'validation-failed';
                while ($('image_file_field').hasClassName(req_class)){
                    $('image_file_field').removeClassName(req_class);
                }
                $('image_file_container').style.display = 'none';
                while ($('image_link_field').hasClassName(req_class)){
                    $('image_link_field').removeClassName(req_class);
                }
                $('image_link_container').style.display = 'none';
                if ($('source_type').value == is_file){
                    $('image_file_field').addClassName(req_class);
                    $('image_file_container').style.display = 'block';
                } else {
                    $('image_link_field').addClassName(req_class);
                    $('image_link_container').style.display = 'block';
                }
                if ($('advice-required-entry-image_link_field')){ $('advice-required-entry-image_link_field').remove(); }
                if ($('advice-required-entry-image_file_field')){ $('advice-required-entry-image_file_field').remove(); }

            }".((!(Mage::registry('image_data') && Mage::registry('image_data')->getId())) ? "
                selectSourceType();": "");
    }

    /**
     * Retrives "Edit from Pending Grid" flag
     * @return boolean
     */
    protected function _isPending()
    {
        return ($this->getRequest()->getParam(AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_PENDING_KEY)
                == AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_PENDING_ROUTE);
    }

    /**
     * Retrives Header text
     * @return string
     */
    public function getHeaderText()
    {
        if(Mage::registry('image_data') && Mage::registry('image_data')->getId()) {
            return Mage::helper('imagetest')->__('Edit Image');
        } else {
            return Mage::helper('imagetest')->__('Add Image');
        }
    }

    /**
     * Get URL for back (reset) button
     * @return string
     */
    public function getBackUrl()
    {
        if ($this->getRequest()->getParam(AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_KEY) == AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_PRODUCT){
            $params = array(
                'id' => $this->getRequest()->getParam('product_id'),
                'tab' => 'product_info_tabs_imagetest',                
            );            
            return $this->getUrl('adminhtml/catalog_product/edit', $params);
        } elseif ($this->getRequest()->getParam(AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_PENDING_KEY) == AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_PENDING_ROUTE) {
            return $this->getUrl('*/*/pending');
        }
        return $this->getUrl('*/*/');
    }

    /**
     * Get URL to delete entry
     * @return string
     */
    public function getDeleteUrl()
    {           
        $params = array($this->_objectId => $this->getRequest()->getParam($this->_objectId));
        if ($this->_isPending()){
            $params[AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_PENDING_KEY] = AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_PENDING_ROUTE;            
        }
        $params[AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_KEY] = AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_EDIT;
        if ($this->getRequest()->getParam(AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_KEY) == AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_PRODUCT){
            $params[AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_KEY] = AW_Imagetest_Block_Adminhtml_Image_Grid::RETURN_FROM_PRODUCT;
            $params['product_id'] = $this->getRequest()->getParam('product_id');
            $params['store'] = $this->getRequest()->getParam('store');
        }
        return $this->getUrl('*/*/delete', $params);
    }
}